package com.wc.blue_bridge_test.base.T58;

import java.util.Scanner;

/**
 * @Author congge
 * @Date 2023/12/29 11:03
 * 矩阵乘法
 * https://lx.lanqiao.cn/problem.page?gpid=T58
 * @description
 */
public class Main {
    static long mod = Long.MAX_VALUE;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        long[][] matrix = new long[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                matrix[i][j] = sc.nextInt();
            }
        }
        matrix = matrixQuickMin(matrix, m);
        print(matrix);
    }

    public static void print(long[][] matrix) {
        StringBuffer ans = new StringBuffer();
        int n = matrix.length;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (j != 0) {
                    ans.append(' ');
                }
                ans.append(matrix[i][j]);
            }
            ans.append('\n');
        }
        System.out.print(ans);
    }

    public static long[][] matrixQuickMin(long[][] matrix, int m) {
        long[][] ans = new long[matrix.length][matrix.length];
        for (int i = 0; i < matrix.length; i++) {
            ans[i][i] = 1;
        }
        while (m > 0) {
            if ((m & 1) == 1) {
                ans = matrixMul(ans, matrix);
            }
            matrix = matrixMul(matrix, matrix);
            m >>= 1;
        }
        return ans;
    }

    public static long[][] matrixMul(long[][] a1, long[][] a2) {
        int n = a1.length;
        int m = a1[0].length;
        int q = a2[0].length;
        long[][] ans = new long[n][q];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < q; j++) {
                for (int k = 0; k < m; k++) {
                    ans[i][j] = (ans[i][j] + a1[i][k] * a2[k][j]) % mod;
                }
            }
        }
        return ans;
    }
}
